package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.service.service.belt.IBeltInfoService;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltInfoReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltInfoRespVO;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltInfoAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltInfo;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltInfoMapper;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 皮带信息（废弃）实现类
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BeltInfoServiceImpl extends ServiceImpl<BeltInfoMapper, BeltInfo> implements IBeltInfoService {

  @Autowired
  private BeltInfoMapper baseMapper;

  @Override
  public BeltInfoRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    BeltInfo entity = baseMapper.selectById(id);
    return BeltInfoAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<BeltInfoRespVO> page(PageRequest<BeltInfoReqDTO> request) {
    BeltInfoReqDTO condition = request.getQueryCondition();
    Page<BeltInfoRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<BeltInfoRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }

  @Override
  public List<BeltInfoRespVO> list(BeltInfoReqDTO reqDto) {
    return baseMapper.queryList(reqDto);
  }

  @Override
  public void add(BeltInfoReqDTO reqDto) {
    BeltInfo entity = BeltInfoAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchAdd(List<BeltInfoReqDTO> dtoList) {
    dtoList.forEach(this::add);
  }

  @Override
  public void update(BeltInfoReqDTO reqDto) {
    BeltInfo oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    BeltInfo newEntity = BeltInfoAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    BeltInfo oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void batchDelete(List<Long> ids) {
    for (Long id : ids) {
      delete(id);
    }
  }

}